/*INIT*/
$(document).ready(function() {
	loadFavorite();
});

function loadFavorite() {
	$.ajax({
        url : getRootPath() + '/customerajax/favor!loadAll.action',
        cache : false, 
        async : true,
        type : "POST",
        dataType : 'json',
        beforeSend	: function(xmlHttpRequest){
	        	loadingArea.attach(".body");
		},
        success : function (result){
        	loadingArea.remove(".body");
        	loadAllFavorite(result);
        	computeResNum(result);
        	restList.init();
        },
        error	:	function(){
        }
    });
}
function loadAllFavorite(data) {
	var item = null;
	var starPoint = null;
	$.each(data.ids, function(i){
		starPoint = parseInt(data.gradePoints[i]);
		item = '<li d-id=\"'+data.fids[i]+
		'\"><div class=\"listItem li_1\"><a href=\"'+getRootPath()+
	    '/'+data.urlNames[i]+
	    '\">'+data.resNames[i]+
	    '</a></div><div class=\"listItem li_2\"><div class=\"star star_'+starPoint+
	    '"></div></div><div class=\"listItem li_3\">'+data.deliveryTimes[i]+
	    '分钟</div><div class=\"listItem li_4\">￥'+data.deliveryLeastValues[i]+
	    '</div><div class=\"listItem li_5\"><a href=\"'+getRootPath()+
	    '/'+data.urlNames[i]+
	    '\">订餐</a> | <a name=\"del\" href=\"javascript:void(0);\">删除</a></div><div class=\"clear\"></div></li>';
		$('.restArea .body').append(item);
	});
}
function computeResNum(data) {
	var tips = '欢迎您，亲爱的'+data.customerName+', 您共收藏了 <em>'+($('.restArea .body li').length-1)+'</em> 家餐厅。';
	$('.tips .body').html(tips);	
}
var restList = {		
		init: function() {
			$('.restArea li [name=del]').click(restList.del);
		},
		del: function() {
			var rest = $(this).parents('li');
			auiAjax.send('rest_del', {
				url: getRootPath() + '/customerajax/favor!deleteFavorite.action',
				data: {id: rest.attr('d-id')},
				type: 'post',
				dataType: 'json',
				success: function(d) {
					if(d.errorCode != '') return;
					$('.restArea li[d-id="'+d.id+'"]').remove();
				}
			});
		}
	};